package org.libarae.server;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author Tyler Holzer
 *
 * ConfigHandler.java -- A part of Libarae
 * Used by the SAX parser to parse config.xml into the ServerConfig object
 */
public class ConfigHandler extends DefaultHandler{
	
	/**
	 * flag for if the element was of type DBType
	 */
	private boolean bDBtype=false;
	/**
	 * flag for  if the element was of type DBExists
	 */
	private boolean bDBexists=false;
	/**
	 * flag for  if the element was of type port
	 */
	private boolean bPort=false;
	/**
	 * flag for  if the element was of type pwdRequired
	 */
	private boolean bpwdRequired=false;
	/**
	 * flag for  if the element was of type ImageFolder
	 */
	private boolean bImageFolder=false;
	/**
	 * flag for  if the element was of type accessKey
	 */
	private boolean bAccessKey=false;
	/**
	 * flag for if the elment was of type debugMode
	 */
	private boolean bDebugMode=false;
	
	/**
	 * ServerConfig object to store important data about the server
	 */
	private ServerConfig config;
	
	/**
	 * Constructor for ConfigHandler
	 * 
	 * @param config - the ServerConfig object that the obtained files should be stored in
	 */
	public ConfigHandler(ServerConfig config) {
		this.config=config;
	}
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		//Check to see what the element is
		if(qName.equalsIgnoreCase("DATABASE_TYPE")) {
			bDBtype=true;
		}
		else if(qName.equalsIgnoreCase("DATABASE_EXISTS")) {
			bDBexists=true;
		}
		else if(qName.equalsIgnoreCase("PORT")) {
			bPort=true;
		}
		else if(qName.equalsIgnoreCase("REQUIRES_PASSWORD")) {
			bpwdRequired=true;
		}
		else if(qName.equalsIgnoreCase("image_folder_created")) {
			bImageFolder=true;
		}
		else if(qName.equalsIgnoreCase("ISBNdb_Access_Key")) {
			this.bAccessKey=true;
		}
		else if(qName.equalsIgnoreCase("DEBUG_MODE")) {
			this.bDebugMode=true;
		}
		
	}
	
	 /* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void endElement(String uri, String localName, String qName) throws SAXException {
	 
	 }
	 

	 /* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	public void characters(char ch[], int start, int length) throws SAXException {
		//depending on what the which element flag is true, set the appropriate field in ServerConig 
		if(bDBtype) {
			 String tmp=new String(ch, start,length);
			 if(tmp.equalsIgnoreCase("sqlite"))
				 config.setDBType(0);
			 else if(tmp.equalsIgnoreCase("mysql"))
				 config.setDBType(1);
			 else if(tmp.equalsIgnoreCase("oracle"))
				 config.setDBType(2);
			 else
				 config.setDBType(-1);
			 bDBtype=false;
		 }
		 else if(bDBexists){
			 String tmp=new String(ch, start,length);
			 if(tmp.equalsIgnoreCase("true"))
			 	config.setDBExists(1);
			 else if(tmp.equalsIgnoreCase("false"))
				 config.setDBExists(0);
			 else 
				 config.setDBExists(-1);
			 bDBexists=false;
		 }
		 else if(bPort) {
			 try{
				 config.setPort(Integer.parseInt(new String(ch, start,length)));
			 }
			 catch(Exception e) {
				 //If the port is wasn't an integer, exit the program
				System.out.println("SERVER ERROR:\nAn error has occured while reading config.xml\nPlease verify that no invalid changes have been made to the file and restart the server");
				System.out.println("If this problem persists, please delete(or rename) the config.xml and reconfigure the server by starting the server again");
				System.exit(-1);
			 }
			 bPort=false;
		 }
		 else if(bpwdRequired) {
			 String tmp=new String(ch, start,length);
			 if(tmp.equalsIgnoreCase("true"))
			 	config.setPasswordRequired(1);
			 else if(tmp.equalsIgnoreCase("false"))
				 config.setPasswordRequired(0);
			 else 
				 config.setPasswordRequired(-1);
			 bpwdRequired=false;
		 }
		 else if(bImageFolder){
			 String tmp=new String(ch, start,length);
			 if(tmp.equalsIgnoreCase("true"))
			 	config.setImagesFolderCreated(1);
			 else if(tmp.equalsIgnoreCase("false"))
				 config.setImagesFolderCreated(0);
			 else 
				 config.setDBExists(-1);
			 bImageFolder=false;
		 }
		 else if(this.bAccessKey){
			 config.setAccessKey(new String(ch,start,length));
			 this.bAccessKey=false;
		 }
		 else if(bDebugMode){
			 String tmp=new String(ch, start,length);
			 if(tmp.equalsIgnoreCase("true"))
			 	config.setDebugMode(1);
			 else if(tmp.equalsIgnoreCase("false"))
				 config.setDebugMode(0);
			 else 
				 config.setDebugMode(-1);
			 bDebugMode=false;
		 }
	 }
}
